from django.conf.urls.defaults import *
from django.contrib.syndication.feeds import Feed
from google.appengine.ext import db
from google.appengine.api import users

import json_custom as json
import sys
import string

#from models import bvr_user, freebase_edit_link


def user_login(rpx_response):
    
    # Convert the rpx_response to text that our JSON interpreter can read:
    rpx_response = rpx_response.replace("'",'"');
    rpx_response = rpx_response.replace("u","");
    sys.stderr.write(rpx_response+'\n')
    
    # Convert the JSON to a python object
    obj = json.read(rpx_response+'\n');
    sys.stderr.write(repr(obj));

    # Obtain the logged-in user's profile
    profile = obj["profile"];
    login_id = profile["identifier"];
    sys.stderr.write("login = " + login_id+'\n')

    # See if this user exists in the database:
#    query = db.GqlQuery("SELECT * FROM bvr_user WHERE login_uid = '"+login_id+"' ")
#    results = query.fetch(1);
#    sys.stderr.write(repr(results)+'\n');

    # if there is no result, create one
#    if len(results) is 0: 
#        sys.stderr.write("no result\n")
#        user = bvr_user(login_uid=login_id);
#        user.put()
#    else :
#        user = results[0]
    
    # Testing functions:
#    user.first_name = "John"
#    user.last_name = "Doe"

#    user.technology_expertise.append("Microfluidics")
#    user.technology_expertise.append("Genomics"); 
    
#    user.medical_condition_expertise.append("Cancer")
#    user.medical_condition_expertise.append("Stroke")
    
#    user.put()
    #sys.stderr.write(repr(user)+'\n');
    
######################################################
# Functions for recording Freebase edit data

# freebase_add_link_edit records newly linked data that a user may have entered
def freebase_add_link_edit(topic1_id, topic1_property, topic2_id):
    # obtain the user current user
    user = get_current_user()
    # create a freebase link object and assign it to the user
    freebase_edit = freebase_edit_link(
                                       user = user,
                                       topic_id = topic1_id,
                                       property_value = topic1_property,
                                       topic2_id = topic2_id)
    # add it to the data store
    sys.stderr.write(repr(freebase_edit)+'\n');
    freebase_edit.put()

    # For testing:
    print_all_freebase_edits(user)

# get_current_user returns the user object from the data store that corresponds to 
#     the currently logged in user
def get_current_user():

    sys.stderr.write('get_current_user\n');    
    # right now you need to manually enter the user_id you want to use.
    login_id = 'https://www.google.com/acconts/o8/id?id=AItOawlHLy0mFoYs7DxOQzAafU1MdnCqHVCkZP8'
    query = db.GqlQuery("SELECT * FROM bvr_user WHERE login_uid = '"+login_id+"' ")
    results = query.fetch(1);
    sys.stderr.write(repr(results)+'\n');
    user = results[0]
    return user;

# Obtains the user's information from the datastore
def get_user_info(user, list_delimiter):
    # first we need to convert the lists to strings.  Use '+++' as a delimiter.
    technology_string = string.join(user.technology_expertise, list_delimiter);
    medical_condition_string = string.join(user.medical_condition_expertise, list_delimiter);
    
    user_info = {
                 'delimiter' : list_delimiter,
                 'first_name' : user.first_name, 
                 'last_name' : user.last_name,
                 'technology_expertise' : technology_string,
                 'medical_condition_expertise' : medical_condition_string
                 }
    return user_info
    
# Prints all of the user's freebase edits to the log.  This function is used for testing
def print_all_freebase_edits(user):
    for f_edit in user.freebase_edit_set:
        sys.stderr.write(repr(f_edit)+'\n')
    
